package com.atguigu.rabbitmq.producer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Author ChenYongSheng
 * Date 2025/11/3 16:25
 */
@Configuration
public class RabbitDLXConfiguration {

    // 交换机名称
    public static final String exchange_delay_order = "exchange.delay.order";
    //路由名称
    public static final String routing_delay_order = "routing.delay.order";
    //队列名称
    public static final String queue_delay_order = "queue.delay.order";

    // 声明死信队列
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable(queue_delay_order).build();
    }

    // 声明一个死信交换机
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(exchange_delay_order, "x-delayed-message", true, false, args);
    }

    // 把死信交换机和死信队列互相绑定
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(dlxQueue()).to(delayExchange()).with(routing_delay_order).noargs();
    }

}
